import {
  ADDADDRESSLIST,
  ADDBROWERLIST,
  ADDSEARCH,
  DEFAULTADDRESSLIST,
  EDITADDRESSLIST,
  LOGUNSUCCESS,
  QUITLOGIN,
  REMOTEBROWERLIST,
  REMOVEADDRESSLIST,
  REMOVESEARCH,
  TOGGLECOLLECTLIST,
} from "./userActions";

const initState = {
  // 用户信息
  userInfo: {
    id: "",
    username: "",
    password: "",
    nickname: "",
    avator: "",
  },
  // 用户收藏数据
  userCollectList: [],
  // 用户浏览数据
  userBrowerList: [],
  // 用户搜索记录
  userSearch: [],
  // 用户收获地址列表
  addressList: [],
};

export const userReducer = (prevState = initState, action) => {
  switch (action.type) {
    // 登录成功
    case LOGUNSUCCESS: {
      return {
        ...prevState,
        userInfo: action.payload,
        addressList: action.payload.addressList,
      };
    }
    // 退出登录
    case QUITLOGIN: {
      localStorage.removeItem("token");
      return {
        ...prevState,
        userInfo: {
          id: "",
          username: "",
          password: "",
          nickname: "",
          avator: "",
        },
      };
    }
    // 加入 或 取消 收藏
    case TOGGLECOLLECTLIST: {
      // baseSpuId
      const item = prevState.userCollectList.find(
        (v) => v.baseSpuId === action.payload.baseSpuId
      );
      if (item) {
        return {
          ...prevState,
          userCollectList: prevState.userCollectList.filter(
            (v) => v.baseSpuId !== action.payload.baseSpuId
          ),
        };
      } else {
        return {
          ...prevState,
          userCollectList: [...prevState.userCollectList, action.payload],
        };
      }
    }

    // 加入浏览记录
    case ADDBROWERLIST: {
      const index = prevState.userBrowerList.findIndex(
        (v) => v.baseSpuId === action.payload.baseSpuId
      );

      if (index > -1) {
        const [item] = prevState.userBrowerList.splice(index, 1);
        return {
          ...prevState,
          userBrowerList: [item, ...prevState.userBrowerList],
        };
      } else {
        return {
          ...prevState,
          userBrowerList: [action.payload, ...prevState.userBrowerList],
        };
      }
    }
    // 删除浏览记录
    case REMOTEBROWERLIST: {
      console.log(123);
      return {
        ...prevState,
        userBrowerList: prevState.userBrowerList.filter(
          (v) => v.baseSpuId !== action.payload.baseSpuId
        ),
      };
    }
    // 添加收获地址
    case ADDADDRESSLIST: {
      return {
        ...prevState,
        addressList: [...prevState.addressList, action.payload],
      };
    }
    // 删除收获地址
    case REMOVEADDRESSLIST: {
      return {
        ...prevState,
        addressList: prevState.addressList.filter(
          (v) => v.id !== action.payload.id
        ),
      };
    }
    // 编辑收获地址
    case EDITADDRESSLIST: {
      const item = prevState.addressList.find(
        (v) => v.id === action.payload.id
      );
      if (item) {
        Object.assign(item, action.payload);
      }
      return {
        ...prevState,
        addressList: [...prevState.addressList],
      };
    }
    // 默认地址
    case DEFAULTADDRESSLIST: {
      const newAddressList = prevState.addressList.filter(
        (v) => v.id !== action.payload.id
      );
      newAddressList.unshift(action.payload);
      return {
        ...prevState,
        addressList: newAddressList,
      };
    }
    // 添加搜索
    case ADDSEARCH: {
      return {
        ...prevState,
        userSearch: [...new Set([action.payload, ...prevState.userSearch])],
      };
    }
    // 删除搜索
    case REMOVESEARCH: {
      return {
        ...prevState,
        userSearch: prevState.userSearch.filter(v => v !== action.payload),
      };
    }
    default:
      return prevState;
  }
};
